﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JMI_Nucleo.View.Form_Base
{
    public partial class FrmBase : Form
    {
        private Control conteudo;
        private Control controle;
   
        public FrmBase()
        {
            InitializeComponent();
        }

        #region Troca visual dos controles
        protected void MudaVisual(Control Controle, byte _status)
        {
            if (Controle is TextBox) // Para TextBox
            {
                if (!Controle.Name.Equals("TxtStatus"))
                {
                    if (_status == 1)
                    {
                        ((TextBox)Controle).BackColor = Color.White;
                        ((TextBox)Controle).Enabled = true;
                    }
                    if (_status == 2)
                    {
                        ((TextBox)Controle).BackColor = Color.Silver;
                        ((TextBox)Controle).Enabled = false;
                    }
                }
            }
            else if (Controle is NumericBox.NumericBox)
            {
                if (_status == 1)
                {
                    ((NumericBox.NumericBox)Controle).BackColor = Color.White;
                    ((NumericBox.NumericBox)Controle).Enabled = true;
                }
                if (_status == 2)
                {
                    ((NumericBox.NumericBox)Controle).BackColor = Color.Silver;
                    ((NumericBox.NumericBox)Controle).Enabled = false;
                }
            }
            else if (Controle is MaskedTextBox)
            {
                if (_status == 1)
                {
                    ((MaskedTextBox)Controle).BackColor = Color.White;
                    ((MaskedTextBox)Controle).Enabled = true;
                }
                if (_status == 2)
                {
                    ((MaskedTextBox)Controle).BackColor = Color.Silver;
                    ((MaskedTextBox)Controle).Enabled = false;
                }
            }
            else if (Controle is DateTimePicker) // Para DateTimePicker
            {
                if (_status == 1)
                {
                    ((DateTimePicker)Controle).BackColor = Color.White;
                    ((DateTimePicker)Controle).Enabled = true;
                }
                if (_status == 2)
                {
                    ((DateTimePicker)Controle).BackColor = Color.Silver;
                    ((DateTimePicker)Controle).Enabled = false;
                }
            }
            else if (Controle is CheckBox) // Para CheckBox
            {
                if (_status == 1)
                {
                    ((CheckBox)Controle).ForeColor = Color.Black;
                    ((CheckBox)Controle).Enabled = true;
                }
                if (_status == 2)
                {
                    ((CheckBox)Controle).ForeColor = Color.Silver;
                    ((CheckBox)Controle).Enabled = false;
                }
            }
            else if (Controle is ComboBox)
            {
                if (_status == 1)
                {
                    ((ComboBox)Controle).BackColor = Color.White;
                    ((ComboBox)Controle).Enabled = true;
                }
                if (_status == 2)
                {
                    ((ComboBox)Controle).BackColor = Color.Silver;
                    ((ComboBox)Controle).Enabled = false;
                }
            }
            else if (Controle is Button)
            {
                if (((Button)Controle).Tag != null && ((Button)Controle).Tag.ToString() == "*")
                {
                    if (_status == 1)
                        ((Button)Controle).Enabled = true;
                    if (_status == 2)
                        ((Button)Controle).Enabled = false;
                }
            }
            else if (Controle is RadioButton)
            {
                if (_status == 1)
                    ((RadioButton)Controle).Enabled = true;
                if (_status == 2)
                    ((RadioButton)Controle).Enabled = false;
            }
        }
        #endregion

        #region Muda controles TexttBox e ComboBox para caixa alta
        protected void CaixaAlta(Control Controle)
        {
            if (Controle is TextBox) 
                ((TextBox)Controle).CharacterCasing = CharacterCasing.Upper;
        }
        #endregion

        protected void DefineLayOut()
        {
             #region Define layout controles
            for (int i = 0; i < this.Controls.Count; i++)
            {
                conteudo = this.Controls[i];
                if (conteudo is Panel && (conteudo as Panel).Name.Equals("pnlConteudo"))
                {
                    for (int j = 0; j < conteudo.Controls.Count; j++)
                    {
                        controle = conteudo.Controls[j];
                        if (controle is Label)
                        {
                            (controle as Label).AutoSize = true;
                            (controle as Label).Font = new System.Drawing.Font("Tahoma", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
                        }
                        else if (controle is TextBox)
                        {
                            (controle as TextBox).BorderStyle = BorderStyle.FixedSingle;
                            if ((controle as TextBox).Name.Equals("TxtCodigo"))
                            {
                                (controle as TextBox).BackColor = SystemColors.ButtonFace;
                                (controle as TextBox).TabStop = false;
                            }
                            else
                            {
                                if ((controle as TextBox).Focused)
                                {
                                    (controle as TextBox).BackColor = Color.LightGreen;
                                    (controle as TextBox).ForeColor = Color.Navy;
                                    (controle as TextBox).Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
                                }
                                else
                                {
                                    (controle as TextBox).BackColor = Color.White;
                                    (controle as TextBox).Font = new Font("Tahoma", 8.25F, FontStyle.Regular);
                                }
                            }
                        }
                    }
                    break;
                }
            }
            #endregion
        }

        protected void TrocaIconeAviso(byte Icone)
        {
            TxtStatus.Dock = DockStyle.None;
            switch (Icone)
            {
                case 1:
                    picAlerta.Visible = false;
                    picAlerta.Dock = DockStyle.None;
                    picInformacao.Visible = true;
                    picInformacao.Dock = DockStyle.Left;
                    TxtStatus.Dock = DockStyle.Fill;
                    TxtStatus.ForeColor = SystemColors.ControlText;
                    TxtStatus.Font = new Font(TxtStatus.Font.Name, TxtStatus.Font.Size, FontStyle.Regular);
                    Color clr = Color.FromArgb(224, 224, 224);
                    TxtStatus.BackColor = clr;
                    break;
                case 2:
                    picInformacao.Visible = false;
                    picInformacao.Dock = DockStyle.None;
                    picAlerta.Visible = true;
                    picAlerta.Dock = DockStyle.Left;
                    TxtStatus.Dock = DockStyle.Fill;
                    TxtStatus.ForeColor = Color.Yellow;
                    TxtStatus.Font = new Font(TxtStatus.Font.Name, TxtStatus.Font.Size, FontStyle.Bold);
                    TxtStatus.BackColor = Color.Red;
                    TxtStatus.Refresh();
                    break;
                case 3:
                    picInformacao.Visible = false;
                    picInformacao.Dock = DockStyle.None;
                    picAlerta.Visible = true;
                    picAlerta.Dock = DockStyle.Left;
                    TxtStatus.Dock = DockStyle.Fill;
                    TxtStatus.ForeColor = Color.Blue;
                    TxtStatus.Font = new Font(TxtStatus.Font.Name, TxtStatus.Font.Size, FontStyle.Bold);
                    TxtStatus.BackColor = Color.Yellow;
                    TxtStatus.Refresh();
                    break;
            }
        }
        public FrmBase(String Titulo)
        {
            lblTitulo.Text = Titulo;
        }

        public string Titulo { get; set; }

        private void BtnFechar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBase_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(13))
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            this.HighlightControlOnFocus = true;
            if (this.HighlightControlOnFocus)
            {
                this.ConfigurarFocoComponentes(this);
            }
            this.Text = String.Empty;
            IniciarControle(false);
        }

        protected virtual void IniciarControle(bool Inicializa = true)
        {
            lblTitulo.Text = Titulo;
            BtnIncluir.Enabled = true;
            BtnAlterar.Enabled = false;
            BtnExcluir.Enabled = false;
            BtnPesquisa.Enabled = true;
            BtnSalvar.Enabled = Inicializa;
            BtnConfirma.SendToBack();
            BtnSalvar.BringToFront(); 
            BtnCancelar.Enabled = Inicializa;            
        }

        protected void Limpar(System.Windows.Forms.Control.ControlCollection Controles)
        {
            foreach (Control controle in Controles)
            {
                if (controle is TextBox) // Para TextBox
                {
                    if (((TextBox)controle).Name == "TxtDataAlteracao")
                    {
                        ((TextBox)controle).Text = "Data Alteração: __/__/____";
                    }
                    else
                        ((TextBox)controle).Text = String.Empty;
                }
                else if (controle is MaskedTextBox)
                    ((MaskedTextBox)controle).Text = String.Empty;
                else if (controle is ComboBox)
                    ((ComboBox)controle).Text = String.Empty;
                else if (controle is DateTimePicker)
                    ((DateTimePicker)controle).Value = DateTime.Today;
                else if (controle is CheckBox)
                    ((CheckBox)controle).Checked = false;
                else if (controle is RadioButton)
                    ((RadioButton)controle).Checked = false;
                else if (controle is NumericBox.NumericBox)
                    ((NumericBox.NumericBox)controle).Text = "0,00";
                else if (controle is PictureBox)
                    ((PictureBox)controle).Image = null;
                Limpar(controle.Controls);
            }            
        }
  
        private void BtnLimpar_Click(object sender, EventArgs e)
        {
            Limpar(this.Controls);
            TrocaIconeAviso(1);
        }

        /// <summary>
        /// Muda a cor de fundo de um campo (habilitado) quando ele está em foco
        /// </summary>
        #region Metodo para controle do Foco
        private bool bolHighlightControlOnFocus = false;
        [Category("Focus")]
        [Description("Define se a cor de fundo de um campo é alterada sempre quando ele estiver em foco.")]
        [DisplayName("HighlightControlOnFocus")]
        public bool HighlightControlOnFocus
        {
            get { return bolHighlightControlOnFocus; }
            set { bolHighlightControlOnFocus = value; }
        }

        private void ConfiguraEnterComponente(object sender, EventArgs e)
        {
            if (sender is Control)
            {
                Control controle = (Control)sender;
                if (controle.Enabled)
                {
                    controle.BackColor = Color.LightGreen;
                }
            }
        }
        private void ConfiguraLeaveComponente(object sender, EventArgs e)
        {
            if (sender is Control)
            {
                Control controle = (Control)sender;
                if (controle.Enabled)
                {
                    controle.BackColor = Color.White;
                }
            }
        }
        private void ConfigurarFocoComponentes(Control controle)
        {
            // Configura o Enter e o Leave do componente
            if ((controle is MaskedTextBox) ||
                 (controle is ComboBox) ||
                 (controle is TextBox) ||
                 (controle is RichTextBox) ||
                 (controle is NumericUpDown))
            {
                controle.Enter += this.ConfiguraEnterComponente;
                controle.Leave += this.ConfiguraLeaveComponente;
            }

            if (controle is DateTimePicker)
            {
                controle.Validated += this.ConfiguraLeaveComponente;
            }

            if (controle is ComboBox)
            {
                ComboBox combo = (ComboBox)controle;
                combo.DropDown += this.ConfiguraLeaveComponente;
            }

            // Chamada recursiva para configurar os controles filhos do item atual
            foreach (Control controleFilho in controle.Controls)
            {
                this.ConfigurarFocoComponentes(controleFilho);
            }
        }
        #endregion        
    }    
}
